package com.wangp.mywrite.inwork.sentinel._1_begin_guide;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import java.util.ArrayList;
import java.util.List;

/***
 * @author wangpeng
 * @since 2022-03-28  12:01
 */
public class BeginGuide {

    public static void main(String[] args) {
        commonFunction();
    }

    /**
     * 通用方法
     */
    private static void commonFunction() {
        while (true) {
            try (Entry entry = SphU.entry("HelloWorld")) {

                System.out.println("hello world");
            } catch (BlockException e) {
                System.out.println("blocked!");
            }
        }
    }

    /**
     * 配置规则
     */
    private static void initFlowRules() {
        List<FlowRule> rules = new ArrayList<>();

        FlowRule rule = new FlowRule();
        rule.setResource("HelloWorld");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        // set limit QPS to 20
        rule.setCount(20);
        rules.add(rule);

        FlowRuleManager.loadRules(rules);
    }
}
